package com.um2.mauction.entity;

import java.io.Serializable;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OneToOne;

@Entity
public class Vente implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	public static int ETAT_INACTIF = 0;
	public static int ETAT_ACTIF = 1;

	@Id
	@GeneratedValue(strategy = GenerationType.AUTO)
	private int sale_id;

	private float montantInitial;
	private float montantReserve;
	private String dateDebut;
	private double duree;
	// active/inactive : selon si l'utilisateur souhaite désactiver
	// temporairement la vente: s'il
	// part en vacance par exemple...
	private int etat;

	// champs relatifs à la transaction
	String modeEnvoi;
	float coutEnvoi;

	// the cmr fields

	@ManyToOne
	@JoinColumn(name="id_user")
	private Utilisateur seller;

	@OneToOne
	private Produit item;

	@OneToMany(cascade = CascadeType.ALL,fetch=FetchType.EAGER, targetEntity = Enchere.class, mappedBy = "sale")
	private Set<Enchere> bids=new HashSet<Enchere>();

	public Vente() {
	}

	public Vente(float montantInitial, float montantReserve, String dateDebut,
			double duree, int etat, Utilisateur seller, Produit item) {
		// this.sale_id = sale_id;
		this.montantInitial = montantInitial;
		this.montantReserve = montantReserve;
		this.dateDebut = dateDebut;
		this.duree = duree;
		this.etat = etat;
		this.seller = seller;
		this.item = item;
	}

	public Vente(float montantInitial, float montantReserve, String dateDebut,
			double duree) {
		// this.sale_id = sale_id;
		this.montantInitial = montantInitial;
		this.montantReserve = montantReserve;
		this.dateDebut = dateDebut;
		this.duree = duree;
		this.etat = 1;
		this.seller = null;
		this.item = null;
	}

	public int getSale_id() {
		return sale_id;
	}

	public void setSale_id(int sale_id) {
		this.sale_id = sale_id;
	}

	public float getMontantInitial() {
		return this.montantInitial;
	}

	public void setMontantInitial(float montantInitial) {
		this.montantInitial = montantInitial;
	}

	public float getMontantReserve() {
		return this.montantReserve;
	}

	public void setMontantReserve(float montantReserve) {
		this.montantReserve = montantReserve;
	}

	public String getDateDebut() {
		return this.dateDebut;
	}

	public void setDateDebut(String dateDebut) {
		this.dateDebut = dateDebut;
	}

	public double getDuree() {
		return this.duree;
	}

	public void setDuree(double duree) {
		this.duree = duree;
	}

	public int getEtat() {
		return this.etat;
	}

	public void setEtat(int etat) {
		this.etat = etat;
	}

	// The access methods for cmr fields

	public Utilisateur getSeller() {
		return this.seller;
	}

	public void setSeller(Utilisateur seller) {
		this.seller = seller;
	}

	public Produit getItem() {
		return this.item;
	}

	public void setItem(Produit item) {
		this.item = item;
	}

	public Set<Enchere> getBids() {
		return this.bids;
	}

	public void setBids(Set<Enchere> bids) {
		this.bids = bids;
	}

	// Business methods
	public void assignEnchere(Enchere e) {
		if (this.bids == null) {
			this.bids = new HashSet<Enchere>();
		}
		e.setSale(this);
		this.bids.add(e);
	}

	public String getModeEnvoi() {
		return modeEnvoi;
	}

	public void setModeEnvoi(String modeEnvoi) {
		this.modeEnvoi = modeEnvoi;
	}

	public float getCoutEnvoi() {
		return coutEnvoi;
	}

	public void setCoutEnvoi(float couteEnvoi) {
		this.coutEnvoi = couteEnvoi;
	}
	
public String toString(){

		return		"\n montantInitial :" 	+ getMontantInitial() +
					"\n  montantReserve: "	+ getMontantReserve() +
					"\n  dateDebut: "		+ getDateDebut() +
					"\n  duree: "			+ getDuree() +
					"\n  etat: "			+ getEtat() +
					"\n  modeEnvoi: "		+ getModeEnvoi() +
					"\n  coutEnvoi:"		+ getCoutEnvoi() +
					"\n  seller: "			+ seller +
					"\n  item: "			+ item;
			
	}
}